﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Kovan.Model.Parameter.UserCenter;
using Kovan.BLL.UserCenter;
using Kovan.Model.Entity.UserCenter;
using Kovan.BLL.Common;
using Kovan.Model.Enums;

namespace Kovan.UI.Areas.Manage.Controllers
{
    public class RoleController : ManageControllerBase
    {
        //
        // GET: /Manage/Role/

        public ActionResult Index(PRole param)
        {
            if (!(param.PageSize > 0))
            {
                param.PageSize = 20;
            }
            if (!(param.PageIndex > 0))
            {
                param.PageIndex = 1;
            }
            var items = BActRole.GetInstances().GetList(param);
            ViewBag.Param = param;
            return View("index", items);
        }

        public ActionResult GetOperate(int? id)
        {
            act_role model = new act_role();
            if (id.HasValue)
            {
                model = BActRole.GetInstances().GetModel(id.Value);
            }
            return View("OperateDialog", model);
        }

        public ContentResult Delete(string ids)
        {
            var statuscode = string.Empty;
            List<string> idss = ids.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
            var result = BActRole.GetInstances().BatchDelete(idss);
            if (result > 0)
            {
                statuscode = GetSucceedCode("保存成功", "role_manage", Url.Content("~/Manage/Role"), "角色管理");
            }
            return Content(statuscode);
        }

        public ActionResult SaveRole(act_role model)
        {
            int result = 0;
            string statuscode = string.Empty;
            if (model.role_id.HasValue)
            {
                var model1 = BActRole.GetInstances().GetModel(model.role_id.Value);
                model1.name = model.name;
                model1.description = model.description;

                result = BActRole.GetInstances().Update(model1);
            }
            else
            {
                model.application_id = 1;
                model.create_date = DateTime.Now;

                result = BActRole.GetInstances().Add(model);
            }

            if (result > 0)
            {
                //成功
                statuscode = GetSucceedCode("保存成功", "role_manage", Url.Content("~/Manage/Role"), "角色管理");
            }
            else
            {
                //失败
                statuscode = GetFailCode("保存失败", "", "", "");
            }
            return Content(statuscode);
        }

        public ActionResult GetAuthority(int? roleid)
        {
            var Menus = BActModule.GetInstances().GetModuleAuthority(new PModule { application_id = WebConfig.CurrentUser.ApplicationID });
            var Authoritys = BUserManage.GetInstances().GetRoleAuthority(roleid.Value);
            ViewBag.RoleId = roleid;
            ViewBag.Menus = Menus;
            ViewBag.Authoritys = Authoritys;
            return View("OperateAuthority");
        }

        public ActionResult SaveAuthority(int? roleid, string authoritys)
        {
            string statuscode = string.Empty;
            if (!roleid.HasValue || roleid.Value == 0)
            {
                statuscode = GetFailCode("操作失败，请选择角色", "", "", "");
                return Content(statuscode);
            }
            BActRoleAuthority.GetInstances().DeleteByRoleID(roleid.Value);

            if (!string.IsNullOrEmpty(authoritys))
            {
                List<string> idss = authoritys.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
                BActRoleAuthority.GetInstances().BatchAdd(roleid.Value, idss);
            }

            statuscode = GetResultCode(new Model.Common.ResultCode<string> { callbackType = "closeCurrent", statusCode = ((int)StatusEnum.Succeed).ToString(), message = "操作成功" });

            return Content(statuscode);
        }
    }
}
